/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.Constants;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.FantasyTeamScoreByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author DeuterioX
 */
public class FantasyTeamScoreByTournamentDateDAO extends AbstractDAO<FantasyTeamScoreByTournamentDate> {

    @Override
    public Class<FantasyTeamScoreByTournamentDate> getPersistentClass() {
        return FantasyTeamScoreByTournamentDate.class;
    }

    public List<FantasyTeamScoreByTournamentDate> getFantasyTeamScoresByTournamentDate( TournamentDate date, int page ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.addOrder( Order.desc( "score" ) );

        mainCriteria.setFirstResult( page * Constants.PAGE_SIZE );
        mainCriteria.setMaxResults( Constants.PAGE_SIZE );

        return mainCriteria.list();
    }

    public List<FantasyTeamScoreByTournamentDate> getFantasyTeamScoresByTournamentDate( TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.addOrder( Order.desc( "score" ) );

        mainCriteria.setFirstResult( 0 );
        mainCriteria.setMaxResults( 100 );

        return mainCriteria.list();
    }

    public int getFantasyTeamPositionByDate( FantasyTeam fantasyTeam , TournamentDate date ) {
        List<FantasyTeamScoreByTournamentDate> equipos;
        int pos = 1;
        
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.addOrder( Order.desc( "score" ) );

        equipos = mainCriteria.list();

        for (FantasyTeamScoreByTournamentDate equipo : equipos) {
            if ( equipo.getFantasyTeam().equals( fantasyTeam ) ) {
                return pos;
            }
            pos++;
        }
        return 0;
    }

    // Ver como debería funcionar el Group By y como manejar la lista que retorna.
    public int getFantasyTeamTotalPositionByDate( FantasyTeam fantasyTeam , TournamentDate date ) {
        List<FantasyTeamScoreByTournamentDate> equipos;
        int pos = 1;

        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        //Restrictions.le equivale a <=
        mainCriteria.add( Restrictions.le( "tournamentDate", date ) );
        mainCriteria.setProjection( Projections.projectionList()
                .add( Projections.sum("score"), "score" )
                .add( Projections.groupProperty("fantasyTeam"), "fantasyTeam" ) );
        mainCriteria.addOrder( Order.desc( "score" ) );
        mainCriteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

        equipos = mainCriteria.list();        

        for (FantasyTeamScoreByTournamentDate equipo : equipos) {
            if ( equipo.getFantasyTeam().equals( fantasyTeam ) ) {
                return pos;
            }
            pos++;
        }
        return 0;
    }

    public FantasyTeamScoreByTournamentDate getFantasyTeamPointsByDate( FantasyTeam fantasyTeam , TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", fantasyTeam ) );

        return (FantasyTeamScoreByTournamentDate) mainCriteria.uniqueResult();
    }

    public List<FantasyTeamScoreByTournamentDate> getFantasyTeamGlobalScoresByTournamentDate( FantasyTeam fantasyTeam , TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamScoreByTournamentDate.class );
        mainCriteria.add( Restrictions.le( "tournamentDate", date ) );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", fantasyTeam ) );

        return mainCriteria.list();
    }
}
